package com.jumei.flume.processor;

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jumei.flume.bo.SensorMessage;
import com.jumei.flume.processor.rules.Type;
import com.jumei.flume.processor.rules.TypeResolver;
import com.jumei.flume.processor.rules.TypeResolver2;
import org.apache.commons.lang.NotImplementedException;

import java.util.List;

public class DefaultExternalProcessor implements ExternalProcessor {

    List<TypeResolver2> urlRules = PageTypeResolver.getUrlRules();//$url
    List<TypeResolver2> referrerRules = PageTypeResolver.getReferrerRules();//$referrer
    List<TypeResolver> gRules = PageTypeResolver.getGeneralRules();
    List<TypeResolver2> clickNameRules = PageTypeResolver.getClickNameRules();//曝光点击
    List<TypeResolver2> viewNameRules = PageTypeResolver.getViewNameRules();//浏览
    List<TypeResolver2> nameRules = Lists.newArrayList();

    public DefaultExternalProcessor() {
        nameRules.addAll(clickNameRules);
        nameRules.addAll(viewNameRules);

    }

    public synchronized void start() {

    }

    public synchronized void stop() {
    }


    @Override
    public List<SensorMessage> process(List<SensorMessage> records, ProcessorContext context) throws Exception {
        throw new NotImplementedException("Not Implemented.");
    }


    @Override
    public void process(SensorMessage record, ProcessorContext context) throws Exception {

        String event = record.getEvent();
        if (Strings.isNullOrEmpty(event)) {//never happen
            return;
        }

        //route for the rule
//		PageTypeResolver.route(record, context);

        context.setParameter("event", event);


        //1. 添加 p_type, p_sub_type, refer_type, refer_sub_type
        if (Objects.equal(event, "$AppViewScreen") || Objects.equal(event, "$pageview")) {

            if (!Strings.isNullOrEmpty(record.getUrl())) {

                //$url解析
                for (TypeResolver2 uOne : urlRules) {
                    if (uOne.filter(record, context)) {
                        Type t = uOne.resolve(record, context);
                        record.setP_type(t.getType());
                        record.setP_sub_type(t.getSubType());

                        break;//规则匹配，跳出
                    }
                }
            }

            if (!Strings.isNullOrEmpty(record.getReferrer())) {


                //$referrer解析
                for (TypeResolver2 rOne : referrerRules) {
                    if (rOne.filter(record, context)) {
                        Type t = rOne.resolve(record, context);
                        record.setRefer_type(t.getType());
                        record.setRefer_sub_type(t.getSubType());

                        break;//规则匹配，跳出
                    }
                }
            }

        } else {
            for (TypeResolver gOne : gRules) {
                if (gOne.filter(record)) {
                    Type t = gOne.resolve(record);
                    record.setP_type(t.getType());
                    record.setP_sub_type(t.getSubType());

                    break;
                    //context.stop();//规则匹配，跳出
                }
            }
        }


        //2. 添加曝光点击来源的中英文名称
        //https://echo.int.jumei.com/issues/158296

        for (TypeResolver2 nOne : nameRules) {
            if (nOne.filter(record, context)) {
                Type t = nOne.resolve(record, context);
                record.setSource_name(t.getCnName());
                record.setSource_pagekey(t.getEnName());

                break;
            }
        }

    }


    public static void main(String[] args) {
        DefaultExternalProcessor defaultExternalProcessor = new DefaultExternalProcessor();
        SensorMessage sensorMessage = new SensorMessage();
        sensorMessage.setEvent("view_material");
        sensorMessage.setCard_type("set_metro");
        sensorMessage.setMaterial_page("home_main");

        ProcessorContext context = new ProcessorContext();
        try {
            defaultExternalProcessor.process(sensorMessage, context);
            System.out.println(sensorMessage.getP_type() + sensorMessage.getP_sub_type());
        } catch (Exception e) {

        }

    }
}
